package com.solution.admin.home.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.home.entity.SiteModuleEntity;
import com.solution.admin.home.service.SiteModuleService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 网站模块web控制层
 * @author llp
 * @date 2017-09-26 14:44:49
 */
@RequestMapping("/siteModule")
@Controller
public class SiteModuleController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SiteModuleController.class);

	@Autowired
	private SiteModuleService siteModuleService;
	
	/**
	 * 跳转网站模块查询页面
	 * @param SiteModule 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/toListSiteModulePage")
	public String toListSiteModulePage(SiteModuleEntity siteModule, HttpServletRequest request) {
		return "home/siteModule_list";
	}

	/**
	 * 分页查询网站模块列表
	 * @param siteModule 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/listSiteModulePage")
	@ResponseBody
	public JSONGrid listSiteModulePage(HttpServletRequest request){
		int pageNum = 1;
        int numPerPage = 10000;
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);
		// 模块名称
        String moduleName = request.getParameter("moduleName");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("moduleName", moduleName);
		try {
			PageBean pageBean = siteModuleService.listSiteModulePage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询网站模块失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/toAddSiteModule")
	public String toAddSiteModule(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		// 在哪个父级目录下添加目录
		request.setAttribute("parentModuleId", request.getParameter("parentModuleId"));
		String parentModuleId = request.getParameter("parentModuleId");
		List<SiteModuleEntity> smeList = new ArrayList<SiteModuleEntity>();
		if(StringUtil.isEmpty(parentModuleId)){
			// 查询像所有节点列表，做为父级节点下拉框的选择数据
			smeList = siteModuleService.listSiteModule(null);
			request.setAttribute("smeList", smeList);
		}else{
			long lid = Long.parseLong(parentModuleId);
			SiteModuleEntity sme = siteModuleService.getSiteModuleById(lid);
			smeList.add(sme);
			request.setAttribute("smeList", smeList);
		}
		return "home/siteModule_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/toUpdateSiteModule")
	public String toUpdateSiteModule(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			SiteModuleEntity siteModule = siteModuleService.getSiteModuleById(id);
			request.setAttribute("siteModule", siteModule);
			
			// 查询像所有节点列表，做为父级节点下拉框的选择数据
			List<SiteModuleEntity> smeList = siteModuleService.listSiteModule(null);
			request.setAttribute("smeList", smeList);
			
		} catch (Exception e) {
			handleException("跳转编辑网站模块页面错误", logger, e);
		}
		return "home/siteModule_form";
	}


	/**
	 * 新增或保存网站模块
	 * @param request 请求对象
	 * @param siteModuleEntity 网站模块实体
	 * @return
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/saveOrUpdateSiteModule")
	@ResponseBody
	public Map<String, Object> saveSiteModule(HttpServletRequest request, SiteModuleEntity siteModuleEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		siteModuleEntity.setCreater(getUserName());
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = siteModuleService.updateSiteModule(siteModuleEntity);
			}else{
				result = (int)siteModuleService.saveSiteModule(siteModuleEntity);
			}
			resultMap.put(PublicStatus.RESULT_CODE, result>0?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, result>0?"操作成功":"操作失败");
			
		} catch (Exception e) {
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
			handleException("添加网站模块失败", logger, e);
		}
		return resultMap;
	}
	
	
	/**
	 * 删除网站模块（逻辑删除）
	 * @param request 请求对象
	 * @param siteModuleEntity 网站模块实体
	 * @return
	 * @author llp
 	 * @date 2017-09-26 14:44:49
	 */
	@RequestMapping("/deleteSiteModuleById")
	@ResponseBody
	public Map<String, Object> deleteSiteModuleById(HttpServletRequest request, SiteModuleEntity siteModuleEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		siteModuleEntity.setCreater(getUserName());
		try {
			String id = request.getParameter("id");
			siteModuleEntity.setIsDelete(PublicStatus.IS_DELETE.DELETED.getId());
			result = siteModuleService.updateSiteModule(siteModuleEntity);
			resultMap.put(PublicStatus.RESULT_CODE, result>0?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, result>0?"操作成功":"操作失败");
			
		} catch (Exception e) {
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
			handleException("删除网站模块", logger, e);
		}
		return resultMap;
	}
	
}
